package org.example;

import org.example.entity.Connection;
import org.example.entity.Shape;
import org.example.factory.*;
import org.example.factory.SimpleFactory;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {

//        simple factory
//        try {
//            Shape shape;
//            String shapeName = "circle";
//            shape = SimpleFactory.drawShape(shapeName);
//            shape.draw();
//            shape.erase();
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }

        //factory method
        FactoryMethod factory = getConnectionBean();
        Connection connection = factory.createConnection();
        connection.getConnection();



    }
    static FactoryMethod getConnectionBean() {
        try {
            //1.get connection name
            String filePath = "D:\\Java\\project\\solfware-designing\\FactoryMethod\\src\\main\\resources\\connection.properties";
            Properties properties = new Properties();
            properties.load(new FileReader(filePath));
            String connection_factory = properties.getProperty("connection_factory");
            System.out.println(connection_factory);

            //2.get factory bean
            Class c = Class.forName(connection_factory);
            Constructor constructor = c.getConstructor(null);
            return (FactoryMethod) constructor.newInstance();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}